/*
 * Copyright 2012-2014 Finbudget.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package finbudgettable.client.ui.grid.cell.string;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.i18n.client.Messages;
import com.google.gwt.user.client.ui.*;

import finbudgettable.client.ui.grid.Utils;
import finbudgettable.client.ui.grid.column.FilterPanel;
import finbudgettable.client.ui.grid.filter.Filter;
import finbudgettable.client.ui.grid.filter.FilterChangeEvent;
import finbudgettable.client.ui.grid.filter.FilterChangeHandler;

public class StringFilterPanel extends VerticalPanel implements FilterPanel {

    interface Strings extends Messages {
        @DefaultMessage("Фильтр (допускает несколько значений через запятую)")
        String filter();

        @DefaultMessage("Строка содержит")
        String value();

        @DefaultMessage("Установить")
        String applyFilter();

        @DefaultMessage("Содержит {0}")
        String contains(String value);
    }

    interface FilterFactory {
        String getOp();
        Filter create(StringFilterPanel filterPanel);
    }

    interface FilterPresenter {
        void show(StringFilterPanel filterPanel, Filter filter);
    }

    static final Strings STRINGS = GWT.create(Strings.class);
    private final TextBox textBox;
    private final String fieldName;

    public StringFilterPanel(String fieldName) {
        this.fieldName = fieldName;
        Label filterLabel = new Label(STRINGS.filter());

        Label valueLabel = new Label(STRINGS.value());

        textBox = new TextBox();

        final Button applyButton = new Button(STRINGS.applyFilter());
        applyButton.setStyleName(Utils.blueButton);
        applyButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                String value = getStringValue();
                fireEvent(new FilterChangeEvent(new ContainStringFilter(StringFilterPanel.this.fieldName, value)));
            }
        });

        textBox.addKeyDownHandler(new KeyDownHandler() {
            @Override
            public void onKeyDown(KeyDownEvent event) {
                if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
                    applyButton.click();
                }
            }
        });


        FlexTable table = new FlexTable();
        table.setStyleName(StringCell.RESOURCES.style().bStringFilterPanel());
        table.setWidget(0, 0, filterLabel);
        table.getFlexCellFormatter().setColSpan(0, 0, 2);
        table.setWidget(1, 0, valueLabel);
        table.setWidget(1, 1, textBox);
        table.setWidget(2, 1, applyButton);
        add(table);
    }

    private String getStringValue() {
        return textBox.getValue();
    }

    @Override
    public void setFilter(Filter filter) {
        if (filter != null && filter instanceof ContainStringFilter) {
            textBox.setText(((ContainStringFilter) filter).getValue());
            return;
        }
        textBox.setText("");
    }

    @Override
    public void addFilterChangeHandler(FilterChangeHandler handler) {
        addHandler(handler, FilterChangeEvent.TYPE);
    }

}
